package com.oyo.common.enums;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * @author zhangyang
 * @date 2018/7/25
 * APP版本号信息
 */
public enum EnumVersion {
    /**
     *UNKNOWN默认0
     */
    UNKNOWN(0),
    /**
     *V100 10000
     */
    V100(10000),
    INFINITY(Integer.MAX_VALUE);

    private final int code;

    EnumVersion(int code) {
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public static EnumVersion create(String lvversion) {
        if (StringUtils.isBlank(lvversion)) {
            return EnumVersion.UNKNOWN;
        }

        String[] split = lvversion.split("\\.");
        int code = 0;
        int level = 2;
        for (String term : split) {
            if (!NumberUtils.isDigits(term)) {
                return EnumVersion.UNKNOWN;
            }

            if (level < 0) {
                break;
            }

            code += Integer.parseInt(term) * Math.pow(100, level);
            level--;
        }

        EnumVersion version = getByCode(code);
        if (version == null) {
            return EnumVersion.UNKNOWN;
        }

        return version;
    }

    private static EnumVersion getByCode(int code) {
        for (EnumVersion version : EnumVersion.values()) {
            if (code == version.getCode()) {
                return version;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(EnumVersion.create("7.10.10").toString());
    }

}
